package com.lul15834.orderfood.activity.manager;
/**
 * 是数据添加书屋的界面
 */

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;
import androidx.activity.EdgeToEdge;
import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.core.content.ContextCompat;
import com.lul15834.orderfood.R;
import com.lul15834.orderfood.dao.FoodDao;
import com.lul15834.orderfood.until.FileImgUntil;

public class ManagerAddFoodActivity extends AppCompatActivity {
    private ActivityResultLauncher<String> getContentLauncher;
    Uri uri;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_manager_add_food);
        Toolbar toolbar=findViewById(R.id.man_addFood_toolbar);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(ManagerAddFoodActivity.this,Manager.class);
                startActivity(intent);
            }
        });
        //实现数据账号共享
        SharedPreferences sharedPreferences=getSharedPreferences("data", Context.MODE_PRIVATE);
        String businessId=sharedPreferences.getString("account","root");//如果这个值没有添加和使用则用默认的
        ImageView img=findViewById(R.id.man_addFood_img);
        getContentLauncher = registerForActivityResult(new ActivityResultContracts.GetContent(), new ActivityResultCallback<Uri>() {
            @Override
            public void onActivityResult(Uri result) {
                if (result != null) {
                    img.setImageURI(result);
                    uri=result;
                } else {
                    Toast.makeText(ManagerAddFoodActivity.this, "未选择图片", Toast.LENGTH_SHORT).show();
                }
            }
        });
        img.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                openGallery(v);
            }
        });
        //获取默认头像
        Drawable defaultDrawable= ContextCompat.getDrawable(this,R.drawable.upimg);
        EditText nameText=findViewById(R.id.man_addFood_name);
        EditText priceText=findViewById(R.id.man_addFood_price);
        EditText desText=findViewById(R.id.man_addFood_des);
        Button addBtn=findViewById(R.id.man_addFood_btn);
        addBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String name=nameText.getText().toString();
                String price=priceText.getText().toString();
                String des=desText.getText().toString();
                if (name.isEmpty()){
                    Toast.makeText(ManagerAddFoodActivity.this,"请输入商品名称",Toast.LENGTH_SHORT).show();
                }else if (price.isEmpty()){
                    Toast.makeText(ManagerAddFoodActivity.this,"请输入商品价格",Toast.LENGTH_SHORT).show();
                }else if (des.isEmpty()){
                    Toast.makeText(ManagerAddFoodActivity.this,"请输入商品描述",Toast.LENGTH_SHORT).show();
                }else {
                    Drawable drawable=img.getDrawable();
                    Bitmap bitmap =((BitmapDrawable) drawable).getBitmap();//获取图片的二进制文件
                    Bitmap bitmapDef =((BitmapDrawable) defaultDrawable).getBitmap();//获取图片的二进制文件
                    //检查Bitmap是否与默认鱼片相同
                    if (bitmap.sameAs(bitmapDef)){//判断是不是默认图片
                        Toast.makeText(ManagerAddFoodActivity.this,"请点击图片进行添加商品图片",Toast.LENGTH_SHORT).show();
                    }else {
                        String path= FileImgUntil.getImgName();//图片存储路径名字
                        FileImgUntil.saveImageBitmapToFileImg(uri,ManagerAddFoodActivity.this,path);//保存图片
                        //准备就绪，协议添加dao
                        int a=FoodDao.addFood(businessId,name,des,price,path);
                        if (a==1){
                            Toast.makeText(ManagerAddFoodActivity.this,"添加商品成功",Toast.LENGTH_SHORT).show();
                        }else {
                            Toast.makeText(ManagerAddFoodActivity.this,"添加商品失败",Toast.LENGTH_SHORT).show();
                        }
                    }
                }
            }
        });
    }
    private void openGallery(View v){
        getContentLauncher.launch("image/*");
    }
}